package com.wzqj.agent.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wanghao@weipass.cn on 2015/8/10.
 * 店铺操作相关工具类
 */
public class StoreUtil {

    /**
     * 生成店铺对象
     * @return
     * @param req
     * @param nickName
     * @param merId
     */
    public static JSONObject generalStore(HttpServletRequest req, String merId,String nickName) {

        JSONObject store = new JSONObject();
        //固定参数 "source":2,"status":1,"logo":"","shortName":"","uid":"41572e00edbbfeadb45f1e56",
        store.put("uid","41572e00edbbfeadb45f1e56");
        store.put("source",2);
        store.put("status",1);
        store.put("logo","");
        store.put("shortName","");
        //app传参
        store.put("name",req.getParameter("shopName"));
        store.put("tel",req.getParameter("tel"));
        store.put("busiLicense",req.getParameter("busiLicense"));
        store.put("busiImgs",req.getParameter("busiImgs"));
        //门店责任人信息
        JSONObject contactUser = new JSONObject();
        contactUser.put("name",req.getParameter("contactUserName"));
        contactUser.put("phone",req.getParameter("contactUserPhone"));
        store.put("contactUser",contactUser);
        //门店地址
        JSONObject address = StoreUtil.generalAddress(req.getParameter("address"));
        store.put("address",address);
        //行业信息格式：id1:dsada,id2:dsada
        JSONArray bizClassify = StoreUtil.generalBizClassify(req.getParameter("bizClassify"));
        store.put("bizClassify",bizClassify);
        //applicant信息
        JSONObject applicant = new JSONObject();
        applicant.put("id",merId);
        applicant.put("name",nickName);
        applicant.put("type",2);
        store.put("applicant",applicant);

        return store;
    }

    /**
     * 更新店铺参数对象
     * @param req
     * @return
     */
    public static JSONObject generalUpdateStore(HttpServletRequest req) {

        JSONObject store = new JSONObject();
        //app传参
        store.put("name",req.getParameter("shopName"));
        store.put("tel",req.getParameter("tel"));
        store.put("busiLicense",req.getParameter("busiLicense"));
        store.put("busiImgs",req.getParameter("busiImgs"));
        //门店责任人信息
        JSONObject contactUser = new JSONObject();
        contactUser.put("name",req.getParameter("contactUserName"));
        contactUser.put("phone",req.getParameter("contactUserPhone"));
        store.put("contactUser",contactUser);
        //门店地址
        JSONObject address = StoreUtil.generalAddress(req.getParameter("address"));
        store.put("address",address);
        //行业信息格式：id1:dsada,id2:dsada
        JSONArray bizClassify = StoreUtil.generalBizClassify(req.getParameter("bizClassify"));
        store.put("bizClassify",bizClassify);

        return store;
    }

    /**
     * 根据地址字符串生成地址对象
     * @param addressstr 格式:北京市,北京市,朝阳区,北京市朝阳区朝阳路管庄京通苑30号楼Plus365商业中心1楼,112.898052,112.898052
     * @return
     */
    public static JSONObject generalAddress(String addressstr) {

        if(StringUtils.isEmpty(addressstr)){
            return null;
        }
        String[] params = addressstr.split(",");
        if(params.length != 6){
            return null;
        }
        JSONObject address = new JSONObject();
        address.put("province",params[0]);
        address.put("city",params[1]);
        address.put("area",params[2]);
        address.put("address_detail",params[3]);
        JSONArray location = new JSONArray();
        location.add(Double.valueOf(params[4]));
        location.add(Double.valueOf(params[5]));
        address.put("location",location);
        return address;
    }

    /**
     * 根据行业列表字符串生成对象
     * @param bizClassifystr 格式: 1:sdad,2:dsada
     * @return
     */
    public static JSONArray generalBizClassify(String bizClassifystr) {

        if(StringUtils.isEmpty(bizClassifystr)){
            return null;
        }
        JSONArray bizClassify = new JSONArray();
        String[] params = bizClassifystr.split(",");
        for (String param : params){
            String[] split = param.split(":");
            if(split.length != 2){
                return null;
            }
            JSONObject classify = new JSONObject();
            classify.put("id",split[0]);
            classify.put("name",split[1]);
            bizClassify.add(classify);
        }
        return bizClassify;
    }


}
